package com.tvunetworks.center.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.SearchBookmarkDeviceParam;
import com.tvunetworks.center.user.model.vo.SearchUserDeviceVo;
import com.tvunetworks.center.user.service.BookmarkDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: BookmarkDeviceController
 * @author lebronchen
 * @create: 2019-06-18 18:56
 **/
@RestController
@RequestMapping("/bookmarkDevice")
@Slf4j
public class BookmarkDeviceController {

    @Autowired
    private BookmarkDeviceService bookmarkDeviceService;

    /**
     *
     * @param param param
     * @return Result
     */
    @GetMapping("/listUnbindDevice")
    public Result<Object> listUnbindDevice(@Valid SearchBookmarkDeviceParam param) {
        List<SearchUserDeviceVo> list = bookmarkDeviceService.listUnbindDevice(param);
        return ResultUtil.success(list);
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @GetMapping("/listBindedDevice")
    public Result<Object> listBindedDevice(@Valid SearchBookmarkDeviceParam param) {
        List<SearchUserDeviceVo> list = bookmarkDeviceService.listBindedDevice(param);

        return ResultUtil.success(list);
    }

    /**
     *
     * @param loginUser loginUser
     * @param bookmarkId  bookmarkId
     * @return Result
     */
    @GetMapping("/listAllBindedDevice")
    public Result<Object> listAllBindedDevice(@Logined LoginUser loginUser, @RequestParam String bookmarkId) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        List<SearchUserDeviceVo> list = bookmarkDeviceService.listAllBindedDevice(loginUser.getId(), bookmarkId);
        List<SearchUserDeviceVo> listToken = bookmarkDeviceService.listBindedDeviceForToken(loginUser.getId(), bookmarkId);

        if (CollectionUtils.isNotEmpty(listToken) && CollectionUtils.isNotEmpty(list)) {
            //查询两个list中的数据是否有重复的,去重操作
            for (int i = 0; i < list.size(); i++) {
                String peerId1 = list.get(i).getPeerId();

                for (int j = 0; j < listToken.size(); j++) {
                    String peerId = listToken.get(j).getPeerId();

                    if (peerId1.equals(peerId)) {
                        list.remove(i);
                        i--;
                    }
                }
            }
            list.addAll(listToken);
        } else if (CollectionUtils.isEmpty(list) && CollectionUtils.isNotEmpty(listToken)) {
            return ResultUtil.success(listToken);
        }
            return ResultUtil.success(list);
    }

    /**
     *
     * @param data data
     * @return Result
     */
    @PostMapping("/correlateBookmarkDevice")
    public Result<Object> correlateBookmarkDevice(@RequestBody JSONObject data) {
        String bookmarkId = data.getString("bookmarkId");
        String addPeerIds = data.getString("addPeerIds");
        String removePeerIds = data.getString("removePeerIds");
        bookmarkDeviceService.correlateBookmarkDevice(bookmarkId, addPeerIds, removePeerIds);
        return ResultUtil.success();
    }

    /**
     *
     * @param loginUser loginUser
     * @param data data
     * @return Result
     */
    @PostMapping("/correlateDeviceBookmark")
    public Result<Object> correlateDeviceBookmark(@Logined LoginUser loginUser, @RequestBody JSONObject data) {
        String bookmarkId = data.getString("bookmarkIds");
        String addPeerIds = data.getString("addPeerId");
        String removePeerIds = data.getString("removeBookmarkIds");
        String bookmarkPid = data.getString("bookmarkPid");
        String name = data.getString("name");

        bookmarkDeviceService.correlateDeviceBookmark(bookmarkId, addPeerIds, removePeerIds, bookmarkPid, name, loginUser.getId());
        return ResultUtil.success();
    }

    /**
     *
     * @param fromBookmarkId fromBookmarkId
     * @param toBookmarkIds toBookmarkIds
     * @param peerIds peerIds
     * @return Result
     */
    @PostMapping("/moveBookmarkDevice")
    public Result<Object> moveBookmarkDevice(@RequestParam String fromBookmarkId, @RequestParam String toBookmarkIds, String peerIds) {
        bookmarkDeviceService.moveBookmarkDevice(fromBookmarkId, toBookmarkIds, peerIds);
        return ResultUtil.success();
    }
}
